// pages/my/myCount/index.js
const app = getApp()
const HttpReq = require('@/utils/HttpReq.js')
import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseUrl: app.globalData.hostIp,
        showNickName: false,
        showImageBox: false,
        showUserName: false,
        headerImage: "",
        name: '',
        nickName: '',
        sex: '',
        birthday: '',
        mobile: '',
        actionShow: false,
        actions: [{
                name: '男',
            },
            {
                name: '女',
            },
        ],
        index: '', //1是性别 2是生日 3是手机4是车牌号5是昵称
        popTitle: '',
        popVal: '',
        code: '',
        fileList: [
            // {
            //   //url: 'https://img.yzcdn.cn/vant/leaf.jpg',
            //   url: 'https://www.nuoyin.cc:8081/img/header/client/451.jpeg',
            //   status: 'success',
            //   message: '上传中',
            // }
        ],
        currentDate: new Date().getTime(),
        minDate: new Date().getTime(),
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            }
            if (type === 'month') {
                return `${value}月`;
            }
            return value;
        },
        dataPopShow: false,
    },

    submit() {
        // if (this.data.name == '') {
        //     Toast.fail('姓名不可为空');
        //     return
        // }
        // if (this.data.nickName == '') {
        //     Toast.fail('昵称不可为空');
        //     return
        // }
        this.setData({
            showUserName: false
        })
        let Data = {
            id: this.data.clientId,
            headerImage: this.data.headerImage,
            name: this.data.name,
            nickName: this.data.nickName,
            phoneNumber: '',
            carCard: '',
            sex: this.data.sex == '男' ? 1 : 0,
            birthday: this.data.birthday
        }
        console.log(Data);
        switch (this.data.index) {
            case '3':
                //手机号
                console.log(this.data.popVal);
                Data.phoneNumber = this.data.popVal;
                break;
            case '4':
                Data.carCard = this.data.popVal;
                break;
            case '5':
                console.log(this.data.popVal);
                Data.nickName = this.data.popVal;
                break;

        }
        console.log(Data);
 
        let url = app.globalData.hostIp + '/weixin/updateClientInfo'

        HttpReq.httpsResquest(url, Data, 'POST').then(res => {
            if (res.code == 1) {
                this.setData({
                    showNickName: false,
                    showImageBox: false,
                    showUserName: false,
                    name: res.data.name,
                    nickName: res.data.nickName,
                    headerImage: res.data.headerImage,
                    sex: res.data.sex == 0 ? '女' : '男',
                    phoneNumber: res.data.phoneNumber,
                    birthday: res.data.birthday
                })
                app.globalData.userInfo = res.data;
                console.log(res);
                Toast.success('修改成功');
            } else {
                Toast.fail('修改失败');
            }
        })
    },
    change(e) {
        console.log(e.currentTarget.dataset.index);
        this.data.index = e.currentTarget.dataset.index;
        //1是性别 2是生日 3是手机
        switch (e.currentTarget.dataset.index) {
            case '1':
                this.setData({
                    actionShow: true
                })
                break;
            case '2':
                this.setData({
                    dataPopShow: true
                })
                break;
            case '3':

                this.setData({
                    showNickName: true,
                    popTitle: '编辑手机号',
                    popVal: this.data.mobile
                })
                break;
            case '4':

                this.setData({
                    showNickName: true,
                    popTitle: '车牌号',
                    popVal: this.data.code
                })
                break;
            default:

                break;

        }

    },
    dataPopClose() {
        this.setData({
            dataPopShow: false
        })
    },
    //生日弹出确认
    datatimeConfirm(e) {


        this.setData({
            birthday: this.dateToTime(e.detail),
            dataPopShow: false
        })
        this.submit()
        // this.dateToTime(e.detail)
    },
    //转换时间
    dateToTime(data) {
        let dates = new Date(data);
        let Y = dates.getFullYear() + '-'
        let M = (dates.getMonth() + 1 < 10 ? '0' + (dates.getMonth() + 1) : dates.getMonth() + 1) + '-';
        let D = (dates.getDate() < 10 ? '0' + dates.getDate() : dates.getDate()) + ' ';
        return Y + M + D;
    },
    //生日弹出取消
    datatimeCancel() {
        this.dataPopClose()
    },
    actionClose() {
        this.setData({
            actionShow: false
        })
    },
    actionSelect(e) {
        console.log(e.detail.name);
        this.setData({
            sex: e.detail.name
        })
        this.submit()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(app.globalData.userInfo)
        this.setData({
            clientId: app.globalData.userInfo.id,
            headerImage: app.globalData.userInfo.headerImage,
            name: app.globalData.userInfo.name,
            nickName: app.globalData.userInfo.nickName,
            sex: app.globalData.userInfo.sex == 1 ? '男' : '女',
            phoneNumber: app.globalData.userInfo.phoneNumber,
            birthday: app.globalData.userInfo.buildTime,
            code: app.globalData.userInfo.carCard,
        })
    },
    afterRead(event) {
        let that = this
        const {
            file
        } = event.detail;
        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        this.setData({
            fileList: [{
                url: null,
                //url: 'https://www.nuoyin.cc:8081/img/header/client/23123-1698209541219.jpg',
                status: 'uploading',
                message: '上传中',
            }]
        })
        wx.uploadFile({
            url: app.globalData.hostIp + '/weixin/HeaderUpload', // 仅为示例，非真实的接口地址
            filePath: file.url,
            name: 'file',
            formData: {
                clientId: this.data.clientId
            },
            success(res) {
                var obj = JSON.parse(res.data);
                //上传完成需要更新 fileList
                if (obj.code == 1) {
                    that.setData({
                        headerImage: obj.data,
                        fileList: [{
                            url: obj.data,
                            status: 'suceess'
                        }]
                    });
                } else {
                    if (obj.msg != null) {
                        Toast.fail(obj.msg);
                    } else {
                        Toast.fail('上传失败');
                    }
                }
            },
        });
    },
    oversize() {
        // wx.showToast({
        //   title: '图片大小最大为1MB',
        //   icon:'error'
        // })
        Toast.fail('图片大小最大为1MB');
    },
    deleteImage(e) {
        console.log(e.detail.index)
        this.setData({
            fileList: {}
        })
    },
    ShowNickName() {

        this.setData({
            showNickName: true,
            popTitle: '编辑昵称',
            popVal: this.data.nickName,
            index: '5'
        })


    },
    CloseImageBox() {
        this.setData({
            showImageBox: false
        })
    },
    //打开上传头像弹框
    ShowImageBox() {
        this.setData({
            showImageBox: true
        })
    },

    CloseNickName() {
        this.setData({
            showNickName: false
        })
    },
    ShowUserName() {
        this.setData({
            showUserName: true,

        })
    },

    CloseUserName() {
        this.setData({
            showUserName: false
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})